<?php
/*
 * Author :pandiam &basanas
 *
 */
require_once ('include/css_head.php');
require_once ('include/functions.php');
require_once ('include/header.php');

require_once ('../BL/BL.php');
require_once ('include/functions.php');
require_once ('../Frameworks/Objects/entity.php');
require_once ('../Frameworks/Objects/attribute.php');
require_once ('../Frameworks/Objects/category.php');

if (!isset($_SESSION['uid']) || $_SESSION['uid'] == -1) {
    UI_registerError('msg_warning', "You are not logged in.");
    header('Location: login.php');
    exit(0);
}

if ( isset($_GET['id']) )
{
    if ( !ctype_digit((string)$_GET['id']) ) {
        UI_registerError('msg_error', "Invalid category request");
        header("location: index.php");
        exit(0);
    }
    $catid = $_GET['id'];
}
else
{
    UI_registerError('msg_warning', "You haven't selected a category to insert entries to!");
    header("location: index.php");
    exit(0);
}

$cat = BL_getCategory($catid);
$entities = BL_selectCategory($catid);
$attributes = $cat->getAttributes();
if ( !empty($attributes) )
    $numAttributes = count($attributes);
else
    $numAttributes = 0;

$i=0;
?>
    <script type="text/javascript" src="js/insertEntityLib.js"></script>

    <div id="msg-box">
        <?php UI_printErrors() ?>
    </div>

    <div id="sideBar" style="float:left">
    </div>

    <div id="profileContent" style="margin-left: 10px;">
    </div>

    <div id="content">
        <form id="insertEntity" name="insertEntity" action="insertEntityDo.php?id=<?php echo $_GET['id']?>" method="post" style="margin-top: 3%;">

    <?php
        if ( $catid != -1 )
        {
    ?>
            <legend>
                Insert Entity
            </legend>
            <table style="margin-top: 1%">
                <tr>
                    <th><h3>Category: <?php echo $cat->GetName();?></h3></th>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                </tr>
                <tr align="right">
                    <td>Entity Name :</td>
                    <td>
                        <input style="margin-top: 3px;" type="text" id="entityName" name="entityName" size="30" maxlength="30"/>
                    </td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td align="center">
                        <h3 align="center">Attributes</h3>
                    </td>
                </tr>
                <?php
                foreach ($attributes as $attr)
                {
                ?>
                <tr align="right">
                    <td><?php echo $attr->GetName();?> :</td>
                    <td>
                        <input style="margin-top: 3px;" type="text" id="attr<?php echo $i;?>" name="attr<?php echo $i;?>" size="30" maxlength="30"/>
                    </td>
                </tr>
                <?php
                    $i++;
                }
                ?>
            </table>

            <input id="attrNum" name="attrNum" type="hidden" value="<?php echo $numAttributes;?>" />

            <input class="button" type="button" onClick="javascript:passData(<?php echo $numAttributes;?>)" value="Insert" />
       </form>

        <?php
            if ( count($entities) > 0 )
            {
        ?>
            <div id="visMatrix">
            <table style=" border:1px solid gray; float:left; margin: 10px auto 10px auto; width:400px;">
                <tr style="border:1px solid gray;">
                    <th style="margin-right:50px;">Entity name</th>
                    <?php
                    foreach ($attributes as $attr)
                    {
                        echo "<th style=\"border:1px solid gray;\">".$attr->GetName()."</th>";
                    }
                    ?>
                    </tr>
                    <?php
                    foreach ($entities as $ent)
                    {
                    ?>
                    <tr style="border:1px solid gray;" >
                        <span class="visGraphText">
                        <?php
                        echo "<td style=\"border:1px solid gray;\">".$ent->getName()."</td>";
                        $attrVal = $ent->getAttributes();
                        foreach ($attrVal as $attr)
                            echo "<td style=\"border:1px solid gray;\">".$attr->GetValue()."</td>";
                        ?>
                        </span>
                    </tr>
                    <?php
                    }
                    ?>
                    </tr>
            </table>
        </div>
        <?php
            }
            else
                echo "This category contains no entities!";
        $i++;
        }
        else {
            echo '<a href="index.php">Find a category of interest</a>';
        }
        ?>
    </div>



<script type="text/javascript">
        $.ajaxSetup ({ cache: false });
        var ajax_load = "<img src='images/load.gif' alt='loading...' />";
        $("#sideBar").load($("#sideBar").html(ajax_load).load('profileSideBar.php'));
</script>

<?php
require_once ('include\footer.php');
?>